//
//  LLQrcodeViewController.m
//  Weibo
//
//  Created by 李灵斌 on 15-1-30.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "LLQrcodeViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "LLMyCardController.h"
@interface LLQrcodeViewController ()<AVCaptureMetadataOutputObjectsDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *qrcodeConsY;

/**
 *  会话
 */
@property (nonatomic, strong) AVCaptureSession *session;
/**
 *  预览界面
 */
@property (nonatomic, weak) AVCaptureVideoPreviewLayer *previewLayer;

@property (nonatomic, strong) CADisplayLink *link;
@property (weak, nonatomic) IBOutlet UILabel *detailLab;
@property (weak, nonatomic) IBOutlet UIButton *myCard;
- (IBAction)myCardClick;

@end

@implementation LLQrcodeViewController
- (IBAction)openAblum:(id)sender {
    
    UIImagePickerController *pc = [[UIImagePickerController alloc] init];
    [self presentViewController:pc animated:YES completion:nil];
}
- (IBAction)close:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad{
    
    [super viewDidLoad];
    
    // 设置detailLab的背景
    UIColor *tipColor = [UIColor colorWithPatternImage:[UIImage stretchableImageWithImage:@"qrcode_tip_background"]];
    self.detailLab.backgroundColor = tipColor;
    
    // 设置myCard背景图片
    [self.myCard setBackgroundImage:[UIImage stretchableImageWithImage:@"qrcode_button_background"] forState:UIControlStateNormal];
    
    
    CADisplayLink *dl = [CADisplayLink displayLinkWithTarget:self selector:@selector(moveQrcode)];
    
    [dl addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    self.link = dl;
    
    // 1,获取输入设置
    AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    // 2，根据输入设置创建输入对象
    AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc] initWithDevice:inputDevice error:NULL];
    if (!input) return;
    // 3,创建输出对象
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    // 4,设置输出对象代理
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

    // 5,创建回话
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    self.session = session;
    // 6,将输入输出添加到会话中
    if ([session canAddInput:input]) {
        [session addInput:input];
    }
    if ([session canAddOutput:output]) {
        [session addOutput:output];
    }
    // 7,设置输出数据类型
    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    // 8.设置预览界面
    AVCaptureVideoPreviewLayer *pre = [AVCaptureVideoPreviewLayer layerWithSession:session];
    pre.frame = self.view.bounds;
    self.previewLayer = pre;
    [self.view.layer insertSublayer:pre atIndex:0];
    [session startRunning];
    
    
}

#pragma mark - AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    
    if (metadataObjects.count >0) {
        // 停止扫描
        [self.session stopRunning];
        [self.previewLayer removeFromSuperlayer];
        // 取出数据
        AVMetadataMachineReadableCodeObject *obj = [metadataObjects lastObject];
        [self.link invalidate];
        self.link = nil;
        // 显示数据
        UILabel *labbel = [[UILabel alloc] initWithFrame:self.view.bounds];
        labbel.numberOfLines = 0;
        labbel.text = obj.stringValue;
        [self.view addSubview:labbel];
    }
    
}

- (void)moveQrcode{
    
    self.qrcodeConsY.constant -= 5;
    if (self.qrcodeConsY.constant <= -170) {
        self.qrcodeConsY.constant = 170;
    }
}

- (IBAction)myCardClick {
    
    LLMyCardController *myCard = [[LLMyCardController alloc] init];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    [self.navigationItem.backBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName :[UIColor whiteColor]} forState:UIControlStateNormal];
    [self.navigationItem.backBarButtonItem setTintColor:[UIColor whiteColor]];
    [self.navigationController pushViewController:myCard animated:YES];
    
}
@end
